﻿using DeployTool.Core;
using System;
using System.IO;
using System.Windows.Forms;

namespace DeployTool.CustomControl
{
    public partial class InitUserControl : UserControl, IDeployStep
    {
        readonly string _rootDir;
        public InitUserControl()
        {
            InitializeComponent();
            _rootDir = Tool.GetRootDir();
        }

        #region IDeployStep
        public StepEnum Step { get; set; } = StepEnum.Init;

        public bool CanNextStep()
        {
            return CheckAspnetcore() || CheckSqlServer() && CheckMemcached();
        }

        public void RefreshData()
        {
            InitButtonState();
        }
        #endregion

        #region private methods
        /// <summary>
        /// 安装aspnetcore sdk
        /// </summary>
        private void InstallAspnetcoreSDK()
        {
            string path = Path.Combine(_rootDir, "environment/dotnet-sdk-3.1.426-win-x64.exe");
            Tool.StartProgramWithNoResult(path);
        }

        /// <summary>
        /// 检测aspnetcore是否部署完毕
        /// </summary>
        private bool CheckAspnetcore()
        {
            string result = Tool.StartProgramWithResult("dotnet", "--info");
            return result.Contains("Microsoft.AspNetCore.App 3.1");
        }

        /// <summary>
        /// 安装sqlserver
        /// </summary>
        private void InstallSqlServer()
        {
            string path = Path.Combine(_rootDir, "environment/SQL2019-SSEI-Expr.exe");
            Tool.StartProgramWithNoResult(path);
        }

        /// <summary>
        /// 检测sqlserver是否部署完毕
        /// </summary>
        private bool CheckSqlServer()
        {
            string result = Tool.StartProgramWithResult("sc", "query MSSQLSERVER");
            if (result.Contains("SERVICE_NAME: MSSQLSERVER"))
            {
                return true;
            }

            result = Tool.StartProgramWithResult("sc", "query SQLEXPRESS");
            return result.Contains("SERVICE_NAME: SQLEXPRESS");
        }

        /// <summary>
        /// 安装 memcached
        /// </summary>
        private void InstallMemcached()
        {
            string path = Path.Combine(_rootDir, "environment/memcached/memcached.exe");
            string result = Tool.StartProgramWithResult(path, "-d install");
            if (!string.IsNullOrWhiteSpace(result))
            {
                MessageBox.Show(result);
            }

            result = Tool.StartProgramWithResult(path, "-d start");
            if (!string.IsNullOrWhiteSpace(result))
            {
                MessageBox.Show(result);
            }
        }

        /// <summary>
        /// 检测memcached是否部署完毕
        /// </summary>
        private bool CheckMemcached()
        {
            string result = Tool.StartProgramWithResult("sc", "query memcached");
            return result.Contains("SERVICE_NAME: memcached");
        }

        /// <summary>
        /// 初始化各个按钮状态
        /// </summary>
        private void InitButtonState()
        {
            bool aspnetcoreState = CheckAspnetcore();
            bool sqlserverState = CheckSqlServer();
            bool memcachedState = CheckMemcached();
            btn_1.Enabled = !aspnetcoreState;
            btn_1.Text = aspnetcoreState ? "已安装" : "未安装";
            btn_2.Enabled = !sqlserverState;
            btn_2.Text = sqlserverState ? "已安装" : "未安装";
            btn_3.Enabled = !memcachedState;
            btn_3.Text = memcachedState ? "已安装" : "未安装";
        }

        private void btn_1_Click(object sender, EventArgs e)
        {
            InstallAspnetcoreSDK();
        }

        private void btn_2_Click(object sender, EventArgs e)
        {
            InstallSqlServer();
        }

        private void btn_3_Click(object sender, EventArgs e)
        {
            InstallMemcached();
        }
        #endregion
    }
}
